<!-- 学段选择 -->
<template>
	<zh-app style="font-family: '阿里巴巴普惠体'">
		<!-- 导航栏 -->
		<zh-navbar title="新生正式报名" color="#85a5ff" linear="right"  />
		<zh-spin :load="data.loading" tip="数据加载中">
		<zh-filterMenu >
			<!-- 年度 -->
			<zh-filterMenu-item isButton  :title="data.year+'年'" un-icon="" icon="tmicon-menu">
				
			</zh-filterMenu-item>
			<!-- 统筹区 -->
			<zh-filterMenu-item :title="data.areaName">
				<view hover-class="opactiy-7" v-for="(item,index) in data.areaCodeDict" :key="index"
					@tap="onSelectArea(item)"
					class="flex-1 flex-row flex flex-row-center-between" style="height: 76rpx;">
					<zh-text :color="item.dictKey===data.areaCode?'primary':''" :label="item.dictValue" />
					<tm-icon :font-size="24" color="primary" v-if="item.dictKey===data.areaCode" name="tmicon-check" />
				</view>	
			</zh-filterMenu-item>
		</zh-filterMenu>
		<!-- 卡片栏 -->
		
		<view class="container flex-center">
			<view class="card flex-center" :style="{backgroundImage: item.bgColor}" @tap="select(item)" v-for="(item, index) in data.cardArr" :key="index">
				<zh-text :font-size="48" color="white" :label="item.name"></zh-text>
			</view>
		</view>
		</zh-spin>
		<!-- 底部学生选择栏 -->
		<zh-action-menu rangKey="fullName" v-model="data.showFlag" :list="data.stuArr" @change="gotoBm"></zh-action-menu>
		<zh-message ref="messageRef" :duration="2000"></zh-message>
		<zh-modal title="系统提示" :content="data.error.message" cancelText="取消" :okText="data.error.code===1?'确认':'前往'" :overlayClick="false" v-model:show="data.showError" @ok="onOk" @cancel="onCancel"></zh-modal>
		
		<zlb-footer></zlb-footer>
	</zh-app>
</template>
<script lang="ts" setup>
	import {ref, reactive, onMounted} from "vue";
	import { onLoad } from "@dcloudio/uni-app";
	// ------------------接收传入参数(组件)-------------------

	// ------------------钩子函数----------------------------
	// 初始化
	onLoad((options:any) => {
		if(options.areaCode){
		  data.areaCode=options.areaCode
		}
		getAreaCode()
		getYear()
	})

	// ------------------变量区------------------------------
	const messageRef=ref()
	// 主数据
	const data = reactive({
		loading:false,
		year:new Date().getFullYear(),
		areaCode:'',
		areaName:'报名区县',
		areaCodeDict:[] as any,
		// 学段
		cardArr: [
			{
				name: '幼儿园',
				value: '1',
				bgColor: 'linear-gradient(to right, #B9EDFA, #3DBAEA)'
				
			},{
				name: '小学',
				value: '2',
				bgColor: 'linear-gradient(to right, #3DBAEA, #1F6ED3)'
			},{
				name: '初中',
				value: '3',
				bgColor: 'linear-gradient(to right, #1F6ED3, #0001A3)'
			},
		],
		// 学生
		stuArr: [] as any,
		// 底部学生选择栏显示标记
		showFlag: false,
		// 选中的学段
		selectCategory: '',
		error:{} as any,
		showError:false,
	})

	// ------------------方法区------------------------------
	//获取可选行政区划
	const getAreaCode=()=>{
	  $api.common.send.post("dict0028", {}).then((res: any) => {
			data.areaCodeDict = res.result||[];
			if(data.areaCodeDict.length>=1){
			  data.areaCode=data.areaCodeDict[0].dictKey
			  data.areaName=data.areaCodeDict[0].dictValue
			}
		}).catch(()=>{})
	}
	//选择行政区划
	const onSelectArea=(item:any)=>{
		data.areaCode=item.dictKey
		data.areaName=item.dictValue
	}
	//获取年度
	const getYear=()=>{
		$api.common.send.post("OperationCache0005", {}).then((res: any) => {
			data.year=res.result.year||new Date().getFullYear()
		}).catch(()=>{})
	}
	// 卡片点击的回调
	const select = async (category: any) => {
			data.loading=true;
			data.selectCategory = category.value;
			//判断是否学生
			let studentRet=await $api.common.send.post("eduStudentEnroll0007",{})
			if(studentRet.result){
				data.loading=false;
				let messageData: any = {}
				messageData.model = 'warn'
				messageData.text = '学生账号不能报名！请登录家长浙里办账号进行报名！'
				messageRef.value.show(messageData)
				return
			}
			//判断是否在业务期范围内
			if(category.value=='1'){
				//幼儿园
				checkOp('1')
			}else{
				//义务段
				checkOp('2')
			}
		
		
	}
	//业务期判断
	const checkOp=(classify:String)=>{
		if(classify=='1'){
			const params={classify:classify,areaCode:data.areaCode,operating:'YRY001'}
			$api.common.send.post("OperationCache0003", params).then((res: any) => {
				let result=res.result
				if(result&&result.flag){
					//跳转到幼儿园学校选择页
					bmYey()
				}else{
					data.loading=false;
					if(result.type==='1'){
						let messageData: any = {}
						messageData.model = 'warn'
						messageData.text = result.messagePrompts
						messageRef.value.show(messageData)
						return
					}else if(result.type==='2'){
						let messageData: any = {}
						messageData.model = 'warn'
						messageData.text = result.messageTip
						messageRef.value.show(messageData)
						return
					}else{
						let messageData: any = {}
						messageData.model = 'warn'
						messageData.text = data.year+'年度幼儿园招生报名工作尚未开始！'
						messageRef.value.show(messageData)
						return
					}
					
				}
			}).catch(()=>{
				data.loading=false;
			})
		}else{
			//义务段
			const params={classify:classify,areaCode:data.areaCode}
			$api.common.send.post("OperationCache0001", params).then((res: any) => {
				
				let result=res.result
				if(result&&result.operation){
					let eduOperation :any=result.operation
					let YW002 :any={}//正式报名 第一轮
					let YW022 :any={}//补报 第二轮
					let YW010 :any={}//抢报 第三轮
					if(eduOperation){
						YW002=eduOperation.YW002
						YW022=eduOperation.YW022
						YW010=eduOperation.YW010
						if(YW002.flag||YW022.flag||YW010.flag){
							//报名中
							bmYw(data.selectCategory)
						}else{
							data.loading=false;
							if(YW002.type==='1'){
								let messageData: any = {}
								messageData.model = 'warn'
								messageData.text = YW002.messagePrompts||data.year+'年度义务教育段招生报名工作尚未开始！'
								messageRef.value.show(messageData)
								return
							}else if(YW002.type==='3'){
								let messageData: any = {}
								messageData.model = 'warn'
								messageData.text = data.year+'年度义务教育段招生报名工作尚未开始！'
								messageRef.value.show(messageData)
								return
							}else{
								if(YW022.type==='1'){
									let messageData: any = {}
									messageData.model = 'warn'
									messageData.text = YW022.messagePrompts||data.year+'年度义务教育段招生补报工作尚未开始！'
									messageRef.value.show(messageData)
									return
								}else if(YW022.type==='3'){
									let messageData: any = {}
									messageData.model = 'warn'
									messageData.text = YW002.messageTip||data.year+'年度义务教育段招生报名已结束！'
									messageRef.value.show(messageData)
									return
								}else{
									if(YW010.type==='1'){
										let messageData: any = {}
										messageData.model = 'warn'
										messageData.text = YW010.messagePrompts||data.year+'年度义务教育段招生第三轮报名尚未开始！'
										messageRef.value.show(messageData)
										return
									}else if(YW010.type==='3'){
										let messageData: any = {}
										messageData.model = 'warn'
										messageData.text = YW022.messageTip||data.year+'年度义务教育段招生报名已结束！'
										messageRef.value.show(messageData)
										return
									}else{
										let messageData: any = {}
										messageData.model = 'warn'
										messageData.text = YW010.messageTip||data.year+'年度义务教育段招生报名已结束！'
										messageRef.value.show(messageData)
										return
									}
								}
							}
						}
					}else{
						data.loading=false;
						let messageData: any = {}
						messageData.model = 'warn'
						messageData.text = data.year+'年度义务教育段招生报名工作尚未开始！'
						messageRef.value.show(messageData)
						return
					}
					
				}else{
					data.loading=false;
					let messageData: any = {}
					messageData.model = 'warn'
					messageData.text = data.year+'年度义务教育段招生报名工作尚未开始！'
					messageRef.value.show(messageData)
					return
				}
			}).catch(()=>{
				data.loading=false;
			})
		}
		
	}
	// 幼儿园报名中处理
	const bmYey = () => {
		data.loading=false;
		$router.push('/pages/edu/nurseryEnroll/schoolList',{})
		
	}
	// 义务段报名中处理
	const bmYw = (item :any) => {
		const params={schoolCategory:data.selectCategory}
		$api.common.send.post("eduStudentEnroll0023", params).then((res: any) => {
			data.loading=false;
			data.stuArr=res.result
			data.showFlag=true
			
		}).catch((err:any)=>{
			data.loading=false;
			openError(err)
			
		})
		
		
	}
	//跳转到一键报名页面
	const gotoBm=(item: any, index: number)=>{
		$router.push('/pages/edu/enroll/yjbmList',{studentEnrollPreId:item.studentEnrollPreId})
	}
	const openError=(error:any)=> {
		data.error=error
		if(error.code>0){
			data.showError=true
		}	
	}
	const onOk=()=>{
		data.showError=false
		if(data.error.code===2){
			//前往我的报名
			$router.replace('/pages/edu/enroll/myEnroll',{})
		}else if(data.error.code===3){
			//前往准新生登记
			$router.replace('/pages/edu/newStuRegister/newStuRegister',{})
		}
	}
	const onCancel=()=>{
		//关闭页面
		data.showError=false
	}
</script>
<style lang="scss" scoped>
	.container {
		 width: 750rpx;
		 height: 1000rpx;
		 padding: 30rpx 0;
		 
		 .card {
		 	height: 190rpx;
			width: 690rpx;
			margin: 40rpx 0;
			border-radius: 8rpx;
			box-shadow: rgb(51 51 51 / 7%) 0px 2px 6px;
		 }
	}
</style>